package inc.jmc.cyberboard.common;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import javax.swing.*;
 
public class DesktopBackgroundPane extends JDesktopPane {
    /**
	 * 
	 */
	private static final long serialVersionUID = -2871371894622628435L;
	private Paint bkground;
    private String wallpaperPath=null;
 
    public DesktopBackgroundPane(URL url) {
        try {
            BufferedImage x = ImageIO.read(url);
            bkground = new TexturePaint(x, new Rectangle(0,0,x.getWidth(), x.getHeight()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public DesktopBackgroundPane() {
    	setDefaultBackground();
    }
    public void setBackground(String filepath) {
        try {
        	wallpaperPath=filepath;
        	FileInputStream fis=new FileInputStream(filepath); 
            BufferedImage x = ImageIO.read(fis);
            fis.close();
            bkground = new TexturePaint(x, new Rectangle(0,0,x.getWidth(), x.getHeight()));
            this.updateUI();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void setDefaultBackground() {
    	try {
    		wallpaperPath=null;
        	InputStream is=ImageLoader.class.getResourceAsStream(ImageLoader.pathResources[ImageLoader.BACKGROUND1]);
            BufferedImage x = ImageIO.read(is);
            is.close();
            bkground = new TexturePaint(x, new Rectangle(0,0,x.getWidth(), x.getHeight()));
            this.updateUI();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
 
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (bkground != null) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(bkground);
            g2.fillRect(0,0,getWidth(), getHeight());
        }
    }
 
    public static void main(String[] args) throws IOException {
         JFrame.setDefaultLookAndFeelDecorated(true);
         //URL url = new URL("http://java.sun.com/people/jag/images/BlueCloudsTile.gif");
         URL url = new URL("file:///C:/mariokart.jpg");
        JDesktopPane pane = new DesktopBackgroundPane(url);
        for(int i=0; i<5; ++i) {
            JInternalFrame jif = new JInternalFrame("frame " + i, true, true, true, true);
            jif.setBounds(i*25,i*25,200,100);
            jif.setVisible(true);
            pane.add(jif);
        }
        JFrame f = new JFrame("DesktopPane");
        f.getContentPane().add(pane);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600,400);
        f.setLocationRelativeTo(null);
        //f.show();
        f.setVisible(true);
    }
	/**
	 * @return the wallpaperPath
	 */
	public String getWallpaperPath() {
		return wallpaperPath;
	}
	/**
	 * @param wallpaperPath the wallpaperPath to set
	 */
	public void setWallpaperPath(String wallpaperPath) {
		this.wallpaperPath = wallpaperPath;
	}
}

